package com.costaccounting.feesys.service.impl;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.*;
import java.util.stream.Collectors;

import com.costaccounting.base.domain.ZsCategory;
import com.costaccounting.base.domain.ZsCategoryPersonConfig;
import com.costaccounting.base.domain.ZsItem;
import com.costaccounting.base.service.IZsCategoryPersonConfigService;
import com.costaccounting.base.service.IZsCategoryService;
import com.costaccounting.base.service.IZsItemService;
import com.costaccounting.common.constant.UserConstants;
import com.costaccounting.common.core.domain.TreeSelect;
import com.costaccounting.common.core.domain.entity.SysDictData;
import com.costaccounting.common.core.domain.entity.SysRole;
import com.costaccounting.common.core.domain.model.ZsCategoryVO;
import com.costaccounting.common.utils.DateUtils;
import com.costaccounting.common.utils.SecurityUtils;
import com.costaccounting.common.utils.StringUtils;
import com.costaccounting.feesys.domain.ZsCategoryPercentage;
import com.costaccounting.feesys.service.IZsCategoryPercentageService;
import com.costaccounting.system.service.ISysDictDataService;
import com.costaccounting.system.service.ISysRoleService;
import com.costaccounting.system.service.ISysUserService;
import org.apache.commons.io.filefilter.SizeFileFilter;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.costaccounting.feesys.mapper.ZsFeeMapper;
import com.costaccounting.feesys.domain.ZsFee;
import com.costaccounting.feesys.service.IZsFeeService;

import javax.swing.*;

/**
 * 费用管理Service业务层处理
 * 
 * @author ruoyi
 * @date 2021-11-08
 */
@Service
public class ZsFeeServiceImpl implements IZsFeeService 
{
    @Autowired
    private ZsFeeMapper zsFeeMapper;
    @Autowired
    private ISysRoleService roleService;
    @Autowired
    private ISysUserService userService;

    @Autowired
    private IZsCategoryService categoryService;
    @Autowired
    private IZsCategoryPersonConfigService configService;
    @Autowired
    private IZsItemService itemService;
    @Autowired
    private ISysDictDataService dataService;
    @Autowired
    private IZsCategoryPercentageService zsCategoryPercentageService;

    /**
     * 查询费用管理
     * 
     * @param id 费用管理主键
     * @return 费用管理
     */
    @Override
    public ZsFee selectZsFeeById(Long id)
    {
        return zsFeeMapper.selectZsFeeById(id);
    }

    /**
     * 查询费用管理列表
     * 
     * @param zsFee 费用管理
     * @return 费用管理
     */
    @Override
    public List<ZsFee> selectZsFeeList(ZsFee zsFee)
    {
        return zsFeeMapper.selectZsFeeList(zsFee);
    }

    /**
     * 新增费用管理
     * 
     * @param zsFee 费用管理
     * @return 结果
     */
    @Override
    public int insertZsFee(ZsFee zsFee)
    {
        zsFee.setCreateTime(DateUtils.getNowDate());
        ZsCategory category = categoryService.selectZsCategoryById(zsFee.getCategoryId());
        if(category!=null){
            zsFee.setAncestors(category.getAncestors());
        }
        String building_no = dataService.selectDictLabel("building_no", zsFee.getFloorId());
        zsFee.setFloorName(building_no);
        zsFee.setCreateBy(SecurityUtils.getUsername());
        zsFeeMapper.insertZsFee(zsFee);
        zsCategoryPercentageService.syncCategoryPercentage(zsFee);
        return 1;
    }
    /**
     * 修改费用管理
     * 
     * @param zsFee 费用管理
     * @return 结果
     */
    @Override
    public int updateZsFee(ZsFee zsFee)
    {
        zsFee.setUpdateTime(DateUtils.getNowDate());
        String building_no = dataService.selectDictLabel("building_no", zsFee.getFloorId());
        zsFee.setFloorName(building_no);
        zsFee.setUpdateBy(SecurityUtils.getUsername());
        zsFeeMapper.updateZsFee(zsFee);
        zsCategoryPercentageService.syncCategoryPercentage(zsFee);
        return 1;
    }

    /**
     * 批量删除费用管理
     * 
     * @param ids 需要删除的费用管理主键
     * @return 结果
     */
    @Override
    public int deleteZsFeeByIds(Long[] ids)
    {
        return zsFeeMapper.deleteZsFeeByIds(ids);
    }

    /**
     * 删除费用管理信息
     * 
     * @param id 费用管理主键
     * @return 结果
     */
    @Override
    public int deleteZsFeeById(Long id)
    {
        return zsFeeMapper.deleteZsFeeById(id);
    }

    @Override
    public List<TreeSelect> getCategoryTreeselect(ZsItem zsItem) {
        List<TreeSelect> treeSelectList=new ArrayList<>();
        String username = SecurityUtils.getUsername();
//        ZsItem itemWhere = new ZsItem();
//        itemWhere.setItemStatus(UserConstants.ITEM_STATUS_0);
//        List<ZsItem> zsItems = itemService.selectZsItemList(itemWhere);
        ZsItem obj = itemService.selectZsItemById(zsItem.getId());
        if(obj==null){
            return treeSelectList;
        }
        //如果是项目经理直接取出有权限的项目下的所有类目,如果是资料员只能看分配的目录
        if(getRoleList().contains(UserConstants.ROLE_REPORTER)){
            //取出当前登录用户拥有的项目权限
            ZsCategoryPersonConfig configWhere=new ZsCategoryPersonConfig();
            configWhere.setLeaderCode(username);
            List<ZsCategoryPersonConfig> zsCategoryPersonConfigs = configService.selectZsCategoryPersonConfigList(configWhere);
            if(StringUtils.isNotEmpty(zsCategoryPersonConfigs)){
                List<ZsCategoryPersonConfig> configList = zsCategoryPersonConfigs.stream().filter(f -> f.getItemId().equals(obj.getId())).collect(Collectors.toList());
                if(StringUtils.isNotEmpty(configList)){
                    ZsCategoryPersonConfig zsCategoryPersonConfig = configList.get(0);
                    //当前用户拥有的类目id集合
                    String categoryIds = zsCategoryPersonConfig.getCategoryId();
                    //取出所有有权限的类目集合
                    List<Long> ids=string2List(categoryIds);
                    return getCategoryList(ids);
                }
            }
        }else{
            String categoryIds = obj.getCategoryIds();
            if(StringUtils.isNotEmpty(categoryIds)){
                List<Long> ids=string2List(categoryIds);
                return getCategoryList(ids);
            }
            return treeSelectList;
        }
        return treeSelectList;
    }

    /**
     * 根据分类id集合，获取所有的分类并组装成分类树结构
     * @param cids
     * @return
     */
    List<TreeSelect> getCategoryList(List<Long> cids){
        List<TreeSelect> treeSelectList=new ArrayList<>();
        //取出所有有权限的类目集合
        ZsCategory categoryWhere=new ZsCategory();
        categoryWhere.setIds(cids);
        List<ZsCategory> zsCategories = categoryService.selectZsCategoryList(categoryWhere);
        //取到每个叶子节点的路径之后，拼接，去重，之后再获取有权限的类目
        List<Long> allIds=new ArrayList<>();
        for (ZsCategory zsCategory : zsCategories) {
            allIds.addAll(string2List(zsCategory.getAncestors()));
        }
        categoryWhere=new ZsCategory();
        categoryWhere.setIds(allIds);
        zsCategories = categoryService.selectZsCategoryList(categoryWhere);
        for (ZsCategory zsCategory : zsCategories) {
            TreeSelect tree=new TreeSelect();
            tree.setId(zsCategory.getId());
            tree.setPid(zsCategory.getPid());
            tree.setLabel(zsCategory.getName());
            ZsCategoryVO category=new ZsCategoryVO();
            BeanUtils.copyProperties(zsCategory,category);
            tree.setCategory(category);
            treeSelectList.add(tree);
        }
        Map<Long, List<TreeSelect>> pListMap = treeSelectList.stream().collect(Collectors.groupingBy(TreeSelect::getPid));
        treeSelectList.stream().forEach(item -> item.setChildren(pListMap.get(item.getId())));
        return pListMap.get(0L);
    }



    @Override
    public List<SysDictData> getFloorList(ZsFee zsFee) {
        ZsItem where=new ZsItem();
        where.setId(zsFee.getItemId());
        where.setItemStatus(zsFee.getStatus());
        List<ZsItem> zsItems = itemService.selectZsItemList(where);
        if(StringUtils.isNotEmpty(zsItems)){
            if(StringUtils.isEmpty(zsItems.get(0).getBuildingIds())){
                return null;
            }
            List<String> bids =Arrays.asList(zsItems.get(0).getBuildingIds().split(","));
            SysDictData dataWhere=new SysDictData();
            dataWhere.setDictType("building_no");
            List<SysDictData> sysDictData = dataService.selectDictDataList(dataWhere);
            List<SysDictData> list = sysDictData.stream().filter(f -> bids.contains(f.getDictValue())).collect(Collectors.toList());
            //如果是资料员则只显示有权限的楼号
            if(getRoleList().contains(UserConstants.ROLE_REPORTER)){
                ZsCategoryPersonConfig cwhere=new ZsCategoryPersonConfig();
                cwhere.setLeaderCode(SecurityUtils.getUsername());
                cwhere.setItemId(zsFee.getItemId());
                List<ZsCategoryPersonConfig> configList = configService.selectZsCategoryPersonConfigList(cwhere);
                if(StringUtils.isNotEmpty(configList)){
                    String buildingIds = configList.get(0).getBuildingIds();
                    if(buildingIds!=null){
                        List<String> arr =Arrays.asList(buildingIds.split(","));
                        list=list.stream().filter(f->arr.contains(f.getDictValue())).collect(Collectors.toList());
                    }
                }
            }
            return list;
        }
        return null;
    }

    @Override
    public ZsItem getActiveItem() {
        ZsItem result=null;
        //获取当前所有激活中的项目
        ZsItem itemWhere = new ZsItem();
        itemWhere.setItemStatus(UserConstants.ITEM_STATUS_0);
        List<ZsItem> zsItems = itemService.selectZsItemList(itemWhere);
        if(StringUtils.isEmpty(zsItems)){
            return null;
        }
        //如果是项目经理，直接获取其激活的项目
        List<String> roleList = getRoleList();
        if(roleList.contains(UserConstants.ROLE_PROJECTMANAGER)
                ||roleList.contains(UserConstants.ROLE_ADMIN)
                ||roleList.contains(UserConstants.ROLE_GENERALMANGER)
                ||roleList.contains(UserConstants.ROLE_BOSS)){
            List<ZsItem> ilist = zsItems.stream().filter(f -> f.getLeaderCode().equals(SecurityUtils.getUsername())).collect(Collectors.toList());
            if(StringUtils.isNotEmpty(ilist)){
                return ilist.get(0);
            }else{
                return null;
            }
        }
        //取出当前登录用户拥有的项目权限
        ZsCategoryPersonConfig configWhere=new ZsCategoryPersonConfig();
        configWhere.setLeaderCode(SecurityUtils.getUsername());
        List<ZsCategoryPersonConfig> zsCategoryPersonConfigs = configService.selectZsCategoryPersonConfigList(configWhere);
        //根据拥有的项目权限找到项目
        List<Long> itemIdList = zsItems.stream().map(ZsItem::getId).collect(Collectors.toList());
        Map<Long, ZsItem> itemMap = zsItems.stream().collect(Collectors.toMap(ZsItem::getId, a -> a));
        if(StringUtils.isNotEmpty(zsCategoryPersonConfigs)) {
            Map<Long, List<ZsCategoryPersonConfig>> map = zsCategoryPersonConfigs.stream().collect(Collectors.groupingBy(ZsCategoryPersonConfig::getItemId));
            for(Long key:map.keySet()){
                if(itemIdList.contains(key)){
                    result = itemMap.get(key);
                    break;
                }
            }
        }
        return result;
    }

    @Override
    public List<String> getRoleList() {
        List<String> list =new ArrayList<>();
        Long userId = SecurityUtils.getUserId();
        List<SysRole> sysRoles = roleService.selectRolesByUserId(userId);
        if(StringUtils.isNotEmpty(sysRoles)){
            list = sysRoles.stream().filter(f->f.isFlag()).map(SysRole::getRoleKey).collect(Collectors.toList());
        }
        return list;
    }

    @Override
    public List<ZsFee> selectZsFeeReport(ZsFee zsFee) {
        return zsFeeMapper.selectZsFeeReport(zsFee);
    }

    @Override
    public List<ZsItem> getItemList() {
        ZsItem where=new ZsItem();
        //如果是项目经理，或资料员，则只能看到自己的项目
        if (getRoleList().contains(UserConstants.ROLE_PROJECTMANAGER)||getRoleList().contains(UserConstants.ROLE_REPORTER)) {
            where.setLeaderCode(SecurityUtils.getUsername());
        }
        List<ZsItem> zsItems = itemService.selectZsItemList(where);
        return zsItems;
    }

    @Override
    public List<ZsFee> selectCostReport(ZsFee zsFee) {
        List<ZsFee> result=new ArrayList<>();
        //获取当前选中节点的分类信息
        ZsCategory category = categoryService.selectZsCategoryById(zsFee.getCategoryId());
        //再依次找出路径下每个分类的费用信息
        //成本预测和实际费用两种
        //取出当前分类的成本的（人工单价、工程量、总价）分析
        List<ZsFee> currentReportList = getCurrentReport(zsFee.getCategoryId(), zsFee.getItemId());
        if(StringUtils.isNotEmpty(currentReportList)){
            ZsFee costFee = getCurrentCategoryFee(currentReportList, "0", zsFee.getFeeCategory(), category.getName());
            ZsFee sjFee = getCurrentCategoryFee(currentReportList, "1", zsFee.getFeeCategory(), category.getName());
            if(costFee!=null){
                String zb=getCurrentParentPercent(category,"0",costFee.getLaborTotal(),zsFee.getFeeCategory());
                costFee.setRemark(zb);
                costFee.setCategoryId(zsFee.getCategoryId());
                result.add(costFee);
            }
            if(sjFee!=null){
                String zb2=getCurrentParentPercent(category,"1",sjFee.getLaborTotal(),zsFee.getFeeCategory());
                sjFee.setRemark(zb2);
                sjFee.setCategoryId(zsFee.getCategoryId());
                result.add(sjFee);
            }
        }
        return result;
    }

    @Override
    public List<ZsFee> selectWarnReport(ZsFee zsFee) {
        List<ZsFee> result=new ArrayList<>();
        ZsFee where=new ZsFee();
        where.setItemId(zsFee.getItemId());
        where.setFloorId(zsFee.getFloorId());
        List<ZsFee> zsFees = this.selectZsFeeList(where);
        if (StringUtils.isNotEmpty(zsFees)){
            List<ZsCategory> zsCategories = categoryService.selectZsCategoryList(new ZsCategory());
            Map<Long,ZsCategory> map=new HashMap<>();
            if(StringUtils.isNotEmpty(zsCategories)){
                map = zsCategories.stream().collect(Collectors.toMap(ZsCategory::getId, a -> a));
            }
            //获取所有用到的分类id
            List<Long> categoryIdList = getCategoryIdList(zsFees);
            for (Long categoryId : categoryIdList) {
                ZsCategory category = map.get(categoryId);
                List<ZsFee> categoryWarn = isCategoryWarn(zsFees, category);
                if(StringUtils.isNotEmpty(categoryWarn)){
                    result.addAll(categoryWarn);
                }
            }
        }
        return result;
    }

    /**
     * 获取费用到到的所有分类id
     * @param feeList
     * @return
     */
    List<Long> getCategoryIdList(List<ZsFee> feeList){
        List<Long> result=new ArrayList<>();
        //先获取所有用到的分类id
        List<String> collect = feeList.stream().map(ZsFee::getAncestors).collect(Collectors.toList());
        List<Long> idList=new ArrayList<>();
        for (String ancestors : collect) {
            idList.addAll(string2List(ancestors));
        }
        result = idList.stream().distinct().collect(Collectors.toList());
        return result;
    }

    /**
     * 每个分类的三个费用是否报警
     * @param zsFees
     * @param category
     * @return
     */
    List<ZsFee> isCategoryWarn(List<ZsFee> zsFees,ZsCategory category){
        List<ZsFee> list=new ArrayList<>();
        //取出包含这个分类的所有费用
        List<ZsFee> categoryFeeList = zsFees.stream().filter(f -> string2List(f.getAncestors()).contains(category.getId())).collect(Collectors.toList());
        //将费用按费用类型分组
        if(StringUtils.isNotEmpty(categoryFeeList)){
            Map<String, List<ZsFee>> map = categoryFeeList.stream().collect(Collectors.groupingBy(ZsFee::getFeeType));
            //计算是否
            ZsFee feeAmount = getFeeAmount(map,"0");
            ZsFee feeAmount1 = getFeeAmount(map,"1");
            //实际费用和成本预测比较
            String b = compareFee(feeAmount1, feeAmount);
            //实际费用各项和工程预算比较
            if(StringUtils.isNotEmpty(b)){
                ZsFee warnCategory = getWarnCategory(feeAmount,UserConstants.FEE_CBYC,category,b);
                list.add(warnCategory);
                ZsFee warnCategory2 = getWarnCategory(feeAmount1,UserConstants.FEE_SJFY,category,b);
                list.add(warnCategory2);
            }
        }
        return list;
    }

    ZsFee getWarnCategory(ZsFee zsFee,String feeType,ZsCategory category,String warnStr){
        ZsFee result=new ZsFee();
        result.setFeeType(feeType);
        result.setCategoryId(category.getId());
        result.setCategoryName(category.getName());
        result.setLaborTotal(zsFee.getLaborTotal());
        result.setMachineryTotal(zsFee.getMachineryTotal());
        result.setMaterialTotal(zsFee.getMaterialTotal());
        result.setRemark(warnStr);
        return result;
    }

    String compareFee(ZsFee zsFee1,ZsFee zsFee2){
        String result="";
         if(zsFee1!=null&&zsFee2!=null){
            //实际费用小于另一个费用时
            if(zsFee1.getLaborTotal().compareTo(zsFee2.getLaborTotal())==1){
                result=result+"0";
//                return true;
            }
            if(zsFee1.getMachineryTotal().compareTo(zsFee2.getMachineryTotal())==1){
                result=result+",2";
//                return true;
            }
            if(zsFee1.getMaterialTotal().compareTo(zsFee2.getMaterialTotal())==1){
                result=result+",1";
//                return true;
            }
        }
        return result;
    }

    /**
     * 获取当前分类的各项费用
     * @param categoryId
     * @param itemId
     * @return
     */
    List<ZsFee> getCurrentReport(Long categoryId,Long itemId){
        List<ZsFee> feeList=new ArrayList<>();
        ZsCategory where=new ZsCategory();
        where.setAncestors(categoryId+"");
        List<ZsCategory> zsCategories = categoryService.selectZsCategoryList(where);
        if(StringUtils.isNotEmpty(zsCategories)){
            List<Long> childIds = zsCategories.stream().map(ZsCategory::getId).collect(Collectors.toList());
            ZsFee fwhere=new ZsFee();
            fwhere.setCategoryIdList(childIds);
            fwhere.setItemId(itemId);
            List<ZsFee> zsFees = this.selectZsFeeReport(fwhere);
            return zsFees;
        }
        return feeList;
    }

    /**
     * 获取每个分类的某项的三个费用
     * @param map
     * @return
     */
    ZsFee getFeeAmount(Map<String,List<ZsFee>> map,String feeType){
        ZsFee result=null;
        if(map.containsKey(feeType)){
            //成本预算
            List<ZsFee> zsFees = map.get(feeType);
            BigDecimal amount0=new BigDecimal("0");
            BigDecimal amount1=new BigDecimal("0");
            BigDecimal amount2=new BigDecimal("0");
            for (ZsFee zsFee : zsFees) {
                if(zsFee.getLaborTotal()!=null){
                    amount0=amount0.add(zsFee.getLaborTotal());
                }
                if(zsFee.getMaterialTotal()!=null){
                    amount1=amount1.add(zsFee.getMaterialTotal());
                }
                if(zsFee.getMachineryTotal()!=null){
                    amount2=amount2.add(zsFee.getMachineryTotal());
                }
            }
            result=new ZsFee();
            result.setLaborTotal(amount0);
            result.setMaterialTotal(amount1);
            result.setMachineryTotal(amount2);
        }
        return result;
    }
    /**
     * 获取到当前分类的单价工程量和总价
     * @return
     */
    ZsFee getCurrentCategoryFee(List<ZsFee> zsFees,String feeType,String feeCategory,String categoryName){
        ZsFee result=null;
        List<ZsFee> collect = zsFees.stream().filter(f -> f.getFeeType().equals(feeType)).collect(Collectors.toList());
        if(StringUtils.isNotEmpty(collect)){
            ZsFee zsFee = collect.get(0);
            ZsFee report=new ZsFee();
            report.setCategoryName(categoryName);
            report.setFeeType(feeType);
            report.setFeeCategory(feeCategory);
            if(feeCategory.equals(UserConstants.FEE_TYPE_LABOR)){
                report.setLaborPrice(zsFee.getLaborPrice());
                report.setLaborNum(zsFee.getLaborNum());
                report.setLaborTotal(zsFee.getLaborTotal());
                result=report;
            }else if(feeCategory.equals(UserConstants.FEE_TYPE_MATERIAL)){
                report.setLaborPrice(zsFee.getMaterialPrice());
                report.setLaborNum(zsFee.getMaterialNum());
                report.setLaborTotal(zsFee.getMaterialTotal());
                result=report;
            }else if(feeCategory.equals(UserConstants.FEE_TYPE_MACHINERY)){
                report.setLaborPrice(zsFee.getMachineryPrice());
                report.setLaborNum(zsFee.getMachineryNum());
                report.setLaborTotal(zsFee.getMachineryTotal());
                result=report;
            }
        }
        return result;
    }

    /**
     * 获取当前选中分类，各占父级每一级的比例
     * @param category
     * @param feeType
     * @param currentAmount
     * @param feeCategory
     * @return
     */
    String getCurrentParentPercent(ZsCategory category,String feeType, BigDecimal currentAmount,String feeCategory){
        //最终呈现方式为：分类1名称[0.5%]|分类2名称[0.3]
        String result="";
        //获取当前选中节点的分类信息
        //找到其父节点的路径信息
        List<Long> paths = string2List(category.getAncestors());
        ZsCategory cwhere=new ZsCategory();
        cwhere.setIds(paths);
        List<ZsCategory> zsCategories = categoryService.selectZsCategoryList(cwhere);
        Map<Long, ZsCategory> categoryMap = zsCategories.stream().collect(Collectors.toMap(ZsCategory::getId, a -> a));
        //取出包含第一个分类的所有费用信息
        ZsFee feeWhere=new ZsFee();
        List<Long> firstCategoryId=new ArrayList<>();
        firstCategoryId.add(paths.get(0));
        feeWhere.setCategoryIdList(firstCategoryId);
        feeWhere.setFeeType(feeType);
        List<ZsFee> zsFees = this.selectZsFeeList(feeWhere);
        if(StringUtils.isNotEmpty(zsFees)){
            //获取每一级的总费用
            for (Long path : paths) {
                if(!category.getId().equals(path)){
                    List<ZsFee> collect = zsFees.stream().filter(f -> string2List(f.getAncestors()).contains(path)).collect(Collectors.toList());
                    if(StringUtils.isNotEmpty(collect)){
                        BigDecimal totalAmount=new BigDecimal(0);
                        if(feeCategory.equals(UserConstants.FEE_TYPE_LABOR)){
                           totalAmount=collect.stream().filter(f -> f.getLaborTotal() != null).map(ZsFee::getLaborTotal).reduce(BigDecimal.ZERO, BigDecimal::add);
                        }else if(feeCategory.equals(UserConstants.FEE_TYPE_MATERIAL)){
                            totalAmount=collect.stream().filter(f -> f.getMaterialTotal() != null).map(ZsFee::getMaterialTotal).reduce(BigDecimal.ZERO, BigDecimal::add);
                        }else if(feeCategory.equals(UserConstants.FEE_TYPE_MACHINERY)){
                            totalAmount=collect.stream().filter(f -> f.getMachineryTotal() != null).map(ZsFee::getMachineryTotal).reduce(BigDecimal.ZERO, BigDecimal::add);
                        }
                        if(totalAmount.compareTo(new BigDecimal(0))!=0){
                            BigDecimal divide = currentAmount.divide(totalAmount, 2, RoundingMode.HALF_UP);
                            if(!result.equals("")){
                                result+=">>";
                            }
                            ZsCategory category1 = categoryMap.get(path);
                            result+=category1.getName()+"["+divide.multiply(new BigDecimal(100))+"%]";
                        }
                    }
                }
            }
        }
        return result;
    }


    List<Long> string2List(String str){
        List<Long> list=new ArrayList<>();
        List<String> strings = Arrays.asList(str.split(","));
        for (String s : strings) {
            list.add(Long.parseLong(s));
        }
        return list;
    }
}
